package com.ruoyi.web.controller.cat;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cat.domain.CatApplycashoutTable;
import com.ruoyi.cat.domain.CatUserbasicTable;
import com.ruoyi.cat.service.ICatApplycashoutTableService;
import com.ruoyi.cat.service.ICatUserbasicTableService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 提现申请Controller
 * 
 * @author ruoyi
 * @date 2024-12-24
 */
@RestController
@RequestMapping("/system/applycashoutTable")
public class CatApplycashoutTableController extends BaseController
{
    @Autowired
    private ICatApplycashoutTableService catApplycashoutTableService;


    @Autowired
    private ICatUserbasicTableService iCatUserbasicTableService;


    /**
     * 查询提现申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatApplycashoutTable catApplycashoutTable)
    {
        startPage();
        List<CatApplycashoutTable> list = catApplycashoutTableService.selectCatApplycashoutTableList(catApplycashoutTable);
        return getDataTable(list);
    }

    /**
     * 导出提现申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "提现申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CatApplycashoutTable catApplycashoutTable)
    {
        List<CatApplycashoutTable> list = catApplycashoutTableService.selectCatApplycashoutTableList(catApplycashoutTable);
        ExcelUtil<CatApplycashoutTable> util = new ExcelUtil<CatApplycashoutTable>(CatApplycashoutTable.class);
        util.exportExcel(response, list, "提现申请数据");
    }

    /**
     * 获取提现申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(catApplycashoutTableService.selectCatApplycashoutTableById(id));
    }

    /**
     * 新增提现申请
     */
    @PreAuthorize("@ss.hasPermi('system:table:add')")
    @Log(title = "提现申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatApplycashoutTable catApplycashoutTable)
    {
        return toAjax(catApplycashoutTableService.insertCatApplycashoutTable(catApplycashoutTable));
    }


    @PostMapping("applycashout")
    public AjaxResult applycashout(@RequestBody CatApplycashoutTable catApplycashoutTable)
    {
        if (catApplycashoutTable.getApplyState().equals("3")){
            CatApplycashoutTable catApplycashoutTable1 =    catApplycashoutTableService.selectCatApplycashoutTableById(catApplycashoutTable.getId());
            catApplycashoutTable1.getUserId();
            CatUserbasicTable catUserbasicTable = iCatUserbasicTableService.selectCatUserbasicTableById(catApplycashoutTable1.getUserId());
            if (catUserbasicTable != null){
                BigDecimal difference =  catUserbasicTable.getWithdrawal().subtract(catApplycashoutTable1.getApplyMoney());
                if (difference.compareTo(BigDecimal.ZERO) < 0) {
                    // 差值等于负数，赋值为0
                    catUserbasicTable.setWithdrawal(BigDecimal.ZERO);
                }else{
                    catUserbasicTable.setWithdrawal(difference);
                }
                catUserbasicTable.getFreezeDeposits().add(catApplycashoutTable1.getApplyMoney());
                iCatUserbasicTableService.updateCatUserbasicTable(catUserbasicTable);
            }
        }

        return toAjax(catApplycashoutTableService.updateCatApplycashoutTable(catApplycashoutTable));
    }

    /**
     * 修改提现申请
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "提现申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatApplycashoutTable catApplycashoutTable)
    {
        return toAjax(catApplycashoutTableService.updateCatApplycashoutTable(catApplycashoutTable));
    }

    /**
     * 删除提现申请
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "提现申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(catApplycashoutTableService.deleteCatApplycashoutTableByIds(ids));
    }
}
